package com.aaa.controller;

import com.aaa.entity.Users;
import com.aaa.service.UsersService;
import com.aaa.util.DefaultMsg;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;
    @Autowired
    PasswordEncoder passwordEncoder;

    @RequestMapping("selectAll")
    public PageInfo<Users> selectAll(String UserName, Integer currentPage, Integer PageSize){
        PageHelper.startPage(currentPage,PageSize);
        QueryWrapper<Users> wrapper=new QueryWrapper<Users>();
        wrapper.like("username",UserName);
        List<Users> list = usersService.list(wrapper);
        PageInfo<Users> pageInfo=new PageInfo<Users>(list);
        return pageInfo;
    }
    @RequestMapping("saveOrUpdate")
    public DefaultMsg saveOrUpdate(Users users){
        if (users.getId()==null){
            users.setPassword(passwordEncoder.encode("123456"));
        }else {
            users.setPassword(passwordEncoder.encode(users.getPassword()));
        }
        boolean b = usersService.saveOrUpdate(users);
        DefaultMsg defaultMsg=new DefaultMsg();
        if (!b){
            defaultMsg.setCode(500);
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }
    @RequestMapping("/delete")
    public DefaultMsg delete(Integer id){
        boolean b = usersService.removeById(id);
        DefaultMsg defaultMsg=new DefaultMsg();
        if (!b){
            defaultMsg.setCode(500);
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }
    @RequestMapping("/czmm")
    public DefaultMsg czmm(Integer id){
        Users users=new Users();
        users.setId(id);
        users.setPassword(passwordEncoder.encode("123456"));
        boolean b = usersService.saveOrUpdate(users);
        DefaultMsg defaultMsg=new DefaultMsg();
        if (!b){
            defaultMsg.setCode(500);
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }
    @RequestMapping("/upload")
    public DefaultMsg upload(MultipartFile file){
        DefaultMsg defaultMsg=new DefaultMsg();
        String dir="D:/upload_374/head/";
        String saveName= UUID.randomUUID().toString();
        File saveFile=new File(dir+saveName);
        try {
            file.transferTo(saveFile);
            defaultMsg.setTarget(saveName);
        } catch (IOException e) {
            e.printStackTrace();
            defaultMsg.setSuccess(0);
            defaultMsg.setError("上传失败");
        }
        return defaultMsg;
    }
}
